<?php

/**
 * 短信发送类
 * @package  module/base/common
 * @version 1.0
 */

quanli_require('module/base/common/quanliHttp.class.php');

class quanliSms {
    private $abc = '';
    /**
     * 生成短信验证码
     * @param int $num
     * @return string
     */
    public function createSmsCode( $num = 6 ) {

        //验证码字符数组
        $n_array = range(0, 9);

        //随机生成$num位验证码字符
        $code_array = array_rand($n_array, $num);

        //重新排序验证码字符数组
        shuffle( $code_array );

        //生成验证码
        $code = implode('', $code_array);

        return $code;

    }

    /**
     * 根据模板别名得到模板内容，并转换已定义的可变数据
     * @param string $template 模板别名
     * @param array $data_array
     * @return mixed|null
     */
    public function getSmsContent( $template, $data_array ) {

        //加载消息模板类
        quanli_include('module/base/system/sysMessageTemplate.class.php');

        //实例消息模板类
        $sysMessageTemplate = new sysMessageTemplate();

        //查询消息模板
        $args_array = array(
            'templateAlias' => $template,
            'type' => 1
        );
        $template_array = $sysMessageTemplate->search( $args_array );
        if (!$template_array) {
            return null;
        }

        //模板消息内容
        $content = $template_array[0]['content'];

        $find_array = array();
        $replace_array = array();

        foreach($data_array as $find => $replace) {
            $find_array[] = $find;
            //$replace_array[] = $replace;

            if($find == '$send_time' ) {
                if(isset($data_array['$send_end_time']) && '00:00:00'!=$data_array['$send_end_time'] && $data_array['$send_end_time'] != $data_array['$send_start_time'] ) {
                    $replace_array[] = $replace . "-" . $data_array['$send_end_time'];
                }
                else {
                    $replace_array[] = $replace . "左右";
                }
            }
            else {
                $replace_array[] = $replace ;
            }

        }


//        if(isset($data_array['$send_end_time']) && !empty($data_array['$send_end_time']))
//        {
//            //$find_array['$send_time']
//        }

        $sms_content = str_replace($find_array, $replace_array, $content);
        return $sms_content;

    }

    /**
     * 发送短信
     * @param int $mobile 手机号
     * @param string $sms_conent 短信内容
     * @return array
     */
    public function sendSMS( $mobile, $sms_conent ) {

       // return array( 'code' => 200, 'message' => '短信发送成功!');

        global $G_SHOP;

        if (!isset($mobile, $sms_conent) || empty($mobile) || empty($sms_conent)) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        //统计发送信息的手机号码当天已发送短信的数量
        $sms_count_array =& $G_SHOP->DBCA->stmt_query('SELECT count(*) AS sms_count FROM log_sms WHERE mobile = ? AND createTime BETWEEN ? AND ? AND sms_status = 1',
                                                       'sss', $mobile, $G_SHOP->sys_date . ' 00:00:00', $G_SHOP->sys_date . ' 23:59:59');
        if ($sms_count_array[0]->sms_count >= 10) {
            //mod by allen.qiang 20141013 系统内用户不限制--start
            $mobile_check_array =& $G_SHOP->DBCA->stmt_query('SELECT count(*) AS usercnt FROM user_master  WHERE mobile = ? AND useFlg =1','s',$mobile);
            if ($mobile_check_array[0]->usercnt == 0)
            {
                return array( 'code' => 400, 'message' => '短信发送次数已超出限制' );
            }
            //mod by allen.qiang 20141013 系统内用户不限制--end
        }

        //记录短信发送记录
        $log_sms = new entity('log_sms');
        $log_sms->creator = 'system';
        $log_sms->mobile = $mobile;
        $log_sms->sms_content = $sms_conent;
        $log_sms->ip = get_ip();
        //生成短信内容
		$mobile = '86' . $mobile;
        $companyUser = '5451';
        $companyPwd = 'bikun@bj123';
		$dc = '15';
        $sms_conent = bin2hex(mb_convert_encoding($sms_conent, 'GBK', 'UTF-8'));
        $sms_str = 'http://esms200.10690007.net/sms/mt?command=MT_REQUEST&spid=' . $companyUser . '&sppassword=' . $companyPwd .
                   '&da=' . $mobile . '&dc=' . $dc . '&sm=' . $sms_conent;

        //发送短信
        $http = new quanliHttp();
        $result_array = $http->get($sms_str);

        //add by allen.qiang 2015年2月28日  FUN-BK-0057-短信功能完善 start
        $str=strstr($result_array['data'],'mtmsgid=');

        if (preg_match('|(\d+)|',$str,$taskId))
           $log_sms->taskId=$taskId[1];
        //add by allen.qiang 2015年2月28日  FUN-BK-0057-短信功能完善 end
        if ($result_array['code'] != 200) {
            $log_sms->sms_status = 2;
            $log_sms->sms_fail_content = print_r($result_array, true);
            $G_SHOP->DBCA->save($log_sms);
            return array( 'code' => 404, 'message' => '短信发放失败' );
        }

        //接收短信
        if (!is_bool(strpos($result_array['data'], 'mterrcode=000'))) {
            $log_sms->sms_status = 1;
            $G_SHOP->DBCA->save($log_sms);
            return array( 'code' => 200, 'message' => '短信发送成功' );
        } else {
            $log_sms->sms_status = 2;
            $log_sms->sms_fail_content = print_r($result_array, true);
            $G_SHOP->DBCA->save($log_sms);
            return array( 'code' => 404, 'message' => '短信发放失败' );
        }

    }

}
